﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Spymaster.Entities;
using Spymaster.Screens.Components;

namespace Spymaster.Screens
{
    public class GameScreen:AbstractScreen
    {
        private float screenWidth;
        public float ScreenWidth
        {
            get { return screenWidth; }
        }
        private float screenHeight;
        public float ScreenHeight
        {
            get { return screenHeight; }
        }

        //private CameraComponent camera;
        //public CameraComponent Camera
        //{
         //   get { return camera; }
        //}

        private ReticuleComponent reticule;

        // List of Entities
        public List<GameEntity> gameEntities;
        public List<GameEntity> GameEntities
        {
            get { return gameEntities; }
        }

        private PlayerEntity player;

        public GameScreen(Game game, GraphicsDeviceManager graphics):
            base(game, graphics)
        {
            gameEntities = new List<GameEntity>();
            screenWidth = graphics.PreferredBackBufferWidth;
            screenHeight = graphics.PreferredBackBufferHeight;
        }

        public override void Initialize()
        {
            player = new PlayerEntity(this);
            gameEntities.Add(player);
            reticule = new ReticuleComponent(this);
            
        }

        public override void LoadContent(ContentManager content)
        {
             player.EntityImage = 
                 content.Load<Texture2D>("Entities/Player/player");
             reticule.Reticule = content.Load<Texture2D>("Entities/reticule");
             PlayerBulletEntity.bulletTexture = content.Load<Texture2D>("Entities/Player/playerbullet");
        }

        public override void Update(GameTime gameTime)
        {
            screenWidth = graphics.PreferredBackBufferWidth;
            screenHeight = graphics.PreferredBackBufferHeight;

            for (int i = 0; i < gameEntities.Count; i++)
            {
                if (gameEntities[i].DestroyOnNextUpdate == false)
                    gameEntities[i].Update(gameTime);
                else
                     gameEntities.RemoveAt(i);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend,
                null, null, null, null, 
                Matrix.CreateTranslation(
                new Vector3(-player.Position.X + (screenWidth / 2.0f), -player.Position.Y + (screenHeight / 2.0f), 0.0f)));

            //reticule.Draw(gameTime, spriteBatch);

            foreach (GameEntity entity in gameEntities)
            {
                entity.Draw(gameTime, spriteBatch);
            }

            spriteBatch.End();
        }

        public void AddEntity(GameEntity newEntity)
        {
            gameEntities.Add(newEntity);
        }
    }
}
